//负责人：监督官
/*
             注册界面（RegisterFrame）:
       组件：账号文本框、两个密码文本框（密码和确认密码）、验证码文本框、用户类型单选按钮（管理员、学生）、注册按钮
       事件：注册按钮点击事件

   */
package com.java.view;


import com.java.datamanage.StudentManageSystem;
import com.java.model.Admin;
<<<<<<< HEAD
import com.java.model.Student;
import picture.Background;
import picture.ButtonUtils;
import picture.WindowUtils;

import java.awt.event.ActionEvent;
import javax.swing.*;
import java.awt.*;
import java.util.Random;

import static com.sun.java.accessibility.util.AWTEventMonitor.addActionListener;

public class RegisterFrame extends JDialog {
    //账号文本框
    private JTextField usernameField;
    //密码文本框
    private JPasswordField passwordField;
    //确认密码文本框
    private JPasswordField confirmPasswordField;
    //验证码文本框
    private JTextField codeField;
    //验证码显示标签
    private JLabel codeLabel;
    //管理员单选按钮
    private JRadioButton adminRadioButton;
    //学生单选按钮
    private JRadioButton studentRadioButton;
    //注册状态
    private boolean registered = false;
    //注册成功的管理员
    private Admin registeredAdmin;
    //注册成功的学生
    private Student registeredStudent;
    private StudentManageSystem system;
    //构造方法
    public RegisterFrame(JDialog parent, StudentManageSystem system) {
        super(parent, "用户注册", true);
        this.system = system;
        Background background = new Background("picture/其他素材/Snipaste_2025-07-18_19-32-30.png");
        background.setBorder(BorderFactory.createEmptyBorder(50, 10, 10, 10));
        setContentPane(background);
        //初始化界面组件
        initComponents();
        //设置布局（7行2列）
        setLayout(new GridLayout(7, 2, 10, 10));
        addComponents();
        pack();
        //相对于父窗口居中
        setLocationRelativeTo(parent);
        //显示logo
        WindowUtils.setLogo(this);

    }
    //初始化组件
    private void initComponents() {
        usernameField = new JTextField(15);
        passwordField = new JPasswordField(15);
        confirmPasswordField = new JPasswordField(15);
        codeField = new JTextField(10);
        //生成随机验证码并显示
        //1.调用generateCode生成验证码
        codeLabel = new JLabel(generateCode());
        codeLabel.setBorder(BorderFactory.createLineBorder(Color.GRAY));
        codeLabel.setPreferredSize(new Dimension(80, 30));
        codeLabel.setHorizontalAlignment(SwingConstants.CENTER);
        //验证码刷新按钮事件
        JButton refreshCodeBtn = new JButton("刷新");
        refreshCodeBtn.addActionListener(e -> {
            codeLabel.setText(generateCode());
        });
        //单选按钮分组,默认学生
        ButtonGroup userTypeGroup = new ButtonGroup();
        adminRadioButton = new JRadioButton("管理员");
        studentRadioButton = new JRadioButton("学生", true);
        userTypeGroup.add(adminRadioButton);
        userTypeGroup.add(studentRadioButton);
    }

    //添加组件到页面
    private void addComponents(){
        add(new JLabel("账号："));
        add(usernameField);
        add(new JLabel("密码："));
        add(passwordField);
        add(new JLabel("确认密码:"));
        add(confirmPasswordField);
        add(new JLabel("验证码："));
        JPanel codePanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 5,0 ));
        codePanel.add(codeField);
        codePanel.add(codeLabel);
        JButton refreshBtn = ButtonUtils.createIconButton("picture/按钮素材/刷新.png", 100, 30);
        if(refreshBtn.getIcon() == null){
            refreshBtn.setText("刷新");
        }
        refreshBtn.addActionListener(e-> codeLabel.setText(generateCode()));
        codePanel.add(refreshBtn);
        add(codePanel);

        add(new JLabel("用户类型"));
        JPanel radioPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 10,0 ));
        radioPanel.add(adminRadioButton);
        radioPanel.add(studentRadioButton);
        add(radioPanel);
        add(new JLabel());//空标签占位
        JButton registerBtn = ButtonUtils.createIconButton("picture/按钮素材/屏幕截图 2025-07-18 164110.png", 100, 30);
        if(registerBtn.getIcon() == null){
            registerBtn.setText("注册");
        }
        registerBtn.addActionListener(this ::  handleRegistration);
        add(registerBtn);
    }
    // 处理注册逻辑
    private void handleRegistration(ActionEvent e) {
        // 获取输入内容
        String username = usernameField.getText().trim();
        String password = new String(passwordField.getPassword());
        String confirmPwd = new String(confirmPasswordField.getPassword());
        String inputCode = codeField.getText().trim();
        String generatedCode = codeLabel.getText();

        // 验证输入
        //1.非空校验，确保密码、账号不为空
        if (username.isEmpty() || password.isEmpty() || confirmPwd.isEmpty()) {
            JOptionPane.showMessageDialog(this, "账号或密码不能为空!", "提示", JOptionPane.WARNING_MESSAGE);
            return;
        }
        //两次输入的密码必须一致
        if (!password.equals(confirmPwd)) {
            JOptionPane.showMessageDialog(this, "两次密码不一致!", "提示", JOptionPane.WARNING_MESSAGE);
            return;
        }
        //验证码校验
        if (!inputCode.equalsIgnoreCase(generatedCode)) {
            JOptionPane.showMessageDialog(this, "验证码错误!", "提示", JOptionPane.WARNING_MESSAGE);
            return;
        }

        // 检查账号是否已存在
        if (system.isUsernameExists(username)) {
            JOptionPane.showMessageDialog(this, "该账号已被注册!", "提示", JOptionPane.WARNING_MESSAGE);
            return;
        }
        // 注册用户
        if (adminRadioButton.isSelected()) {
            registeredAdmin = new Admin(username, password);
            system.updateAdminInfo(registeredAdmin.getUsername(), registeredAdmin.getPassword());
            registered = true;
        } else {
            registeredStudent = new Student(password, username);
            registeredStudent.setName(username);
            system.addStudent(registeredStudent);
            registered = true;
        }

        JOptionPane.showMessageDialog(this, "注册成功!", "提示", JOptionPane.INFORMATION_MESSAGE);
        dispose(); // 关闭注册窗口
    }

    // 生成4位随机验证码
    private String generateCode() {
        String chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        Random random = new Random();
        StringBuilder code = new StringBuilder();
        for (int i = 0; i < 4; i++) {
            code.append(chars.charAt(random.nextInt(chars.length())));
        }
        return code.toString();
    }


    public  boolean isRegistered() {
        return registered;
    }

    public  Admin getRegisteredAdmin() {
        return registeredAdmin;
    }

    public Student getRegisteredStudent() {
        return registeredStudent;
    }

}
=======

import javax.swing.*;

public class RegisterFrame {
    private JTextField usernameField;
    private JPasswordField passwordField;
    private boolean registered = false;
    private Admin registeredAdmin;
    private StudentManageSystem system;

    public RegisterFrame(Object o, StudentManageSystem system) {

    }

    public static void setVisible(boolean b) {
    }

    public static boolean isRegistered() {
      return false;
    }

    public static Admin getRegisteredAdmin() {
        return null;
    }



}
>>>>>>> 1985484ee1452c7d22a8520a807bdae2011c4a07
